package com.campus.controller;

import com.campus.common.ResponseResult;
import com.campus.model.Class;
import com.campus.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc: 班级控制器
 * @author: hjm
 * @date: 2024/11/22
 */
@RestController
@RequestMapping("/class")
public class ClassController {

    private final ClassService classService;

    @Autowired
    public ClassController(ClassService classService) {
        this.classService = classService;
    }

    /**
     * 创建班级
     *
     * @param cls 班级对象
     * @return 创建结果
     */
    @PostMapping("/create")
    public ResponseResult<String> createClass(@RequestBody Class cls) {
        int result = classService.createClass(cls);
        return result > 0 ? ResponseResult.ok("班级创建成功") : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "班级创建失败");
    }

    /**
     * 更新班级信息
     *
     * @param id  班级ID
     * @param cls 班级对象
     * @return 更新结果
     */
    @PutMapping("/{id}/update")
    public ResponseResult<String> updateClass(@PathVariable int id, @RequestBody Class cls) {
        cls.setId(id);
        int result = classService.updateClass(cls);
        return result > 0 ? ResponseResult.ok("班级更新成功") : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "班级更新失败");
    }

    /**
     * 将学生添加到班级
     *
     * @param classId    班级ID
     * @param studentIds 学生ID列表
     * @return 添加结果
     */
    @PostMapping("/{classId}/add-students")
    public ResponseResult<String> addStudentsToClass(@PathVariable int classId, @RequestBody List<Integer> studentIds) {
        int result = classService.addStudentsToClass(classId, studentIds);
        return result > 0 ? ResponseResult.ok("学生添加成功") : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "学生添加失败");
    }

    /**
     * 将教师分配到班级
     *
     * @param classId    班级ID
     * @param teacherIds 教师ID列表
     * @return 分配结果
     */
    @PostMapping("/{classId}/assign-teachers")
    public ResponseResult<String> assignTeachersToClass(@PathVariable int classId, @RequestBody List<Integer> teacherIds) {
        int result = classService.assignTeachersToClass(classId, teacherIds);
        return result > 0 ? ResponseResult.ok("教师分配成功") : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "教师分配失败");
    }
}